package com.shinektv.vod.ksclib;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class KscLine {


    private static final String TAG = "KscLine";
    /**
     * 行中文字信息
     */
    private List<KscSegment> word_list = new ArrayList<>();
    /**
     * 行的开始时间
     */
    private long line_start_time = 0;
    /**
     * 行的结束时间
     */
    private long line_stop_time = 0;
    /**
     * 行的文字内容
     */
    private String line_text = "";
    /**
     * 行索引
     */
    private int line_index = -1;

    public long getLine_start_time() {
        return line_start_time;
    }

    public long getLine_stop_time() {
        return line_stop_time;
    }

    public List<KscSegment> getWord_list() {
        return word_list;
    }

    public String getLine_text() {
        return line_text;
    }

    /**
     * 初始化行内容
     *
     * @param index      行索引
     * @param start_time 00:34.627
     * @param end_time   00:38.705
     * @param text       闭上眼睛忍住呼吸
     * @param wordtimes  374,434,367,367,500,601,601,834
     */
    public void init(int index
            , final String start_time
            , final String end_time
            , final String text
            , final String wordtimes) {
        this.line_index = index;
        this.line_start_time = strConvertoTime(start_time);
        this.line_stop_time = strConvertoTime(end_time);
        this.line_text = text;
        String[] wordtimestext = wordtimes.split(",");
        if (wordtimestext.length == text.length()) {
            //每行的时间间隔，要和字数一一对映
            long detalTime = 0;
            for (int wordIndex = 0; wordIndex < text.length(); wordIndex++) {
                long curDurationTime = Integer.valueOf(wordtimestext[wordIndex]).longValue();
                KscSegment segment = new KscSegment(this.line_start_time + detalTime
                        , curDurationTime
                        , line_text.charAt(wordIndex) + ""
                        , this.line_index
                        , wordIndex);
                detalTime += curDurationTime;

                word_list.add(segment);

            }
        } else {
            Log.e("KscLine", "text:" + text + "  wordtimes:" + wordtimes);
        }

    }

    public void printString() {
        String str = String.format("第%d行 开始时间:%d, 结束时间:%d 歌词内容:%s", line_index, line_start_time, line_stop_time, line_text);
        Log.i(TAG, str);
    }

    /**
     * 时间格式转化为ms  00:34.627
     *
     * @param time
     * @return
     */
    private long strConvertoTime(final String time) {
        //00:34.627
        int pos = time.lastIndexOf(".");
        long result = 0;
        try {

            if (pos != -1) {
                String ms = time.substring(pos + 1);
                result = Integer.valueOf(ms).longValue();
                String[] list = time.substring(0, pos).split(":");
                if (list.length == 2) {
                    //分钟
                    long m = Integer.valueOf(list[0]).longValue();
                    long s = Integer.valueOf(list[1]).longValue();
                    result += m * 60 * 1000 + s * 1000;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
